package com.joseph.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @description 
 * @author Joseph_Mok
 * @date 2016年3月28日下午2:28:02
 */
public class TankProxy implements InvocationHandler {

	private Object target;
	
	public Object getInstance(Object target) {
		this.target = target;
		
		return Proxy.newProxyInstance(this.getClass().getClassLoader(), 
				this.target.getClass().getInterfaces(),
				this);
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		System.out.println("before");
		method.invoke(target, args);
		System.out.println("after");
		return null;
	}
	
	public static void main(String[] args) {
		TankProxy proxy = new TankProxy();
		Movable m = (Movable) proxy.getInstance(new Tank());
		m.run();
		m = (Movable) proxy.getInstance(new Bike());
		m.run();
	}

}
